package data

import (
	"gitee.com/linxing_3/sr-core/domain/model"
	"gitee.com/linxing_3/sr-core/domain/model/vo"
	"gitee.com/mkwchecking/sr-admin/apps/admin/entity"
	"github.com/spf13/cast"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
)

func ResetMenuData(db *gorm.DB) error {
	menus := make([]*entity.SysMenu, 0)

	menus = append(menus, genMenu("100001", "系统管理", "{\"en-US\": \"System\"}", "D", "Menu", "", "/sys", "/route/sys", "Layout", 0, "", 1, 0, "", "2023-12-29 19:19:23.000", "2024-06-19 18:26:23.855", nil, "", "169208825386106918", "", -1, nil, -1, nil))
	menus = append(menus, genMenu("10000101", "用户管理", "{\"en-US\": \"User\"}", "M", "Menu", "100001", "/sys/user", "/route/sys/user", "domain/admin/views/sysUser/index", 0, "", 1, 0, "", nil, "2024-06-19 18:26:39.112", nil, "", "169208825386106918", "", -1, "SysUser", -1, nil))
	menus = append(menus, genMenu("10000102", "菜单管理", "{\"en-US\": \"Menu\"}", "M", "Menu", "100001", "/sys/menu", "/route/sys/menu", "domain/admin/views/sysMenu/index", 0, "", 1, 0, "", nil, "2024-06-19 18:26:44.028", nil, "", "169208825386106918", "", nil, "SysMenu", nil, nil))
	menus = append(menus, genMenu("158603756278120559", "部门管理", "{\"en-US\": \"Department\"}", "M", "Menu", "100001", "", "/route/sys/dept", "domain/admin/views/sysDept/index", 0, "", 1, 0, "", nil, "2024-06-19 18:26:49.639", nil, "", "169208825386106918", "", 0, "SysDept", nil, nil))
	menus = append(menus, genMenu("158612601863929967", "开发工具", "{\"en-US\": \"Devtools\"}", "D", "Menu", "", "", "/route/devTools", "Layout", 0, "", 1, 0, "", "2023-12-31 13:14:37.915", "2023-12-31 13:14:37.915", nil, "", "", "", 0, nil, nil, nil))
	menus = append(menus, genMenu("158612798794891375", "代码生成", "{\"en-US\": \"Code Gen\"}", "M", "Menu", "158612601863929967", "", "/route/devTools/devTable", "domain/devtools/devTable/index", 0, "", 1, 0, "", "2023-12-31 13:16:35.294", "2023-12-31 13:16:35.294", nil, "", "", "", 0, nil, nil, nil))
	menus = append(menus, genMenu("159073149227368559", "表详情", nil, "M", "", "158612601863929967", "", "/route/devtools/devTable/details", "domain/devtools/devTable/DevTableDetails", 0, "", 1, 0, "", "2024-01-03 17:29:45.540", "2024-01-03 17:29:45.540", nil, "", "", "", 1, nil, nil, nil))
	menus = append(menus, genMenu("160045839228600431", "角色管理", "{\"en-US\": \"Role\"}", "M", "AlarmClock", "100001", "", "/route/sys/sysRole", "domain/admin/views/sysRole/index", 0, "", 1, 0, "", nil, "2024-06-19 18:26:54.511", nil, "", "169208825386106918", "", 0, "SysRole", nil, nil))
	menus = append(menus, genMenu("160062980795400303", "职位管理", "{\"en-US\": \"Post\"}", "M", "Comment", "100001", "", "/route/sys/sysPost", "domain/admin/views/sysPost/index", 0, "", 1, 0, "", nil, "2024-06-19 18:27:00.258", nil, "", "169208825386106918", "", 0, "SysPost", nil, nil))
	menus = append(menus, genMenu("160204358502318191", "字典管理", "{\"en-US\": \"Dictionary\"}", "M", "MapLocation", "100001", "", "/route/sys/sysDictGroup", "domain/admin/views/sysDictGroup/index", 0, "", 1, 0, "", nil, "2024-06-19 18:27:09.907", nil, "", "169208825386106918", "", 0, "SysDict", nil, nil))
	menus = append(menus, genMenu("160205651237142639", "字典数据管理", "{\"en-US\": \"Dict Value\"}", "M", "Aim", "100001", "", "/route/sys/sysDictValue", "domain/admin/views/sysDictValue/index", 0, "", 1, 0, "", nil, "2024-02-21 09:49:56.882", nil, "", "", "", 1, "SysDictData", nil, nil))
	menus = append(menus, genMenu("160209138616369263", "系统配置", "{\"en-US\": \"System\"}", "M", "SetUp", "100001", "", "/route/sys/sysConfig", "domain/admin/views/sysConfig/index", 0, "", 1, 0, "", nil, "2024-06-19 18:27:18.839", nil, "", "169208825386106918", "", 0, "SysConfig", nil, nil))
	menus = append(menus, genMenu("160473130895147119", "首页", "{\"en-US\": \"Home\"}", "M", "HomeFilled", "", "", "/home/index", "about/index", 0, "", 1, 0, "", "2024-01-13 09:17:19.000", "2024-01-13 09:18:35.461", nil, "", "", "", 0, "Home", nil, nil))

	return db.Clauses(clause.OnConflict{DoNothing: true}).CreateInBatches(&menus, len(menus)).Error

}

func newVoMap(val any) *vo.JSONMap {
	if val == nil {
		return nil
	}
	n := &vo.JSONMap{}
	n.UnmarshalJSON([]byte(cast.ToString(val)))
	return n
}

func genMenu(menuid, title string, lngtitle any, menutype, icon string, pid any, path, routePath, component string, isFrame int, frameUrl string, status int, order int, tenant_id any, createdAt, updatedAt, deletedAt any, created_by, updated_by, deleted_by any, isHide any, routeName any, mobileTab, mobileTabOrder any) *entity.SysMenu {
	m := &entity.SysMenu{
		MenuId:    cast.ToString(menuid),
		Title:     title,
		LngTitle:  newVoMap(lngtitle),
		MenuType:  entity.MenuType(menutype),
		Icon:      icon,
		Pid:       cast.ToString(pid),
		Path:      path,
		RoutePath: routePath,
		Component: component,
		IsFrame:   isFrame == 1,
		FrameUrl:  frameUrl,
	}
	m.Status = model.Status(status)
	m.Order = order

	if isHide != nil {
		m.IsHide = cast.ToInt(isHide)
	}
	if routeName != nil {
		m.RouteName = cast.ToString(routeName)
	}
	if mobileTab != nil {
		m.MobileTab = cast.ToInt(mobileTab)
	}
	if mobileTabOrder != nil {
		m.MobileTabOrder = cast.ToInt(mobileTabOrder)
	}
	return m
}
